{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.instrument import IRSwaption\n",
    "from gs_quant.markets.portfolio import Portfolio\n",
    "from gs_quant.session import GsSession"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret\n",
    "GsSession.use(client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Solve for Present Value\n",
    "Using 1x2 receiver structure - solve for strike on second leg such that pv of leg2 = pv of leg1\n",
    "\n",
    "This example takes advantage of intra-portfolio formulae. You could do this in two API requests with:\n",
    "```python\n",
    "swaption_1 = IRSwaption( ... )\n",
    "price = swaption_1.price()\n",
    "swaption_2 = IRSwaption( ..., strike=f'{price}/pv', ... )\n",
    "zero_cost_portfolio = Portfolio((swaption_1, swaption_2))\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%# buy 1x atmf payer, sell 2x atmf+X payer solving for 0 cost strike\n"
    },
    "tags": [
     "Instrument - Solving across multiple instruments"
    ]
   },
   "outputs": [],
   "source": [
    "zero_cost_portfolio = Portfolio(\n",
    "    (\n",
    "        IRSwaption(\n",
    "            'Receive',\n",
    "            '30y',\n",
    "            'USD',\n",
    "            notional_amount=10e6,\n",
    "            expiration_date='3m',\n",
    "            strike='atmf',\n",
    "            buy_sell='Buy',\n",
    "            name='30y_buy',\n",
    "        ),\n",
    "        IRSwaption(\n",
    "            'Receive',\n",
    "            '30y',\n",
    "            'USD',\n",
    "            notional_amount=20e6,\n",
    "            expiration_date='3m',\n",
    "            strike='=solvefor([30y_buy].risk.Price,pv)',\n",
    "            buy_sell='Sell',\n",
    "        ),\n",
    "    )\n",
    ")\n",
    "\n",
    "# see the strikes and prices\n",
    "print([s.strike * 1e4 for s in zero_cost_portfolio.resolve(in_place=False)])\n",
    "print([s for s in zero_cost_portfolio.price()])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
